<?php 
$page_title = "21 Game - Your games";
include("header.php");
if(!isset($_SESSION['username'])){
	header("Location: index.php");
} ?>

<!-- page header -->
<div class="page-header">
	<div class="container">
		<h1>Your Games</h1>
	</div>
</div>
<!-- end page header -->
<div class="container">
<?php 
	require_once("sql/connection.php");
	require_once("utilities/functions.php");

	$r = yourGames($dbc, $_SESSION['username']);

	if(isset($_GET['cancel']) AND $_GET['cancel'] == 1 AND isset($_GET['pid'])){

		cancelChallenge($dbc, $_GET['gameid'], $_GET['pid']);
	}
	else if(isset($_GET['reject']) AND $_GET['reject'] == 1 AND isset($_GET['pid'])){
		$_GET['pid'] = getPlayerId($dbc, $_GET['pid']);
		cancelChallenge($dbc, $_GET['gameid'], $_GET['pid']);
	}

	if(isset($r)){
		echo "<div class='col-lg-6'>
				<table class='table table-hover'>
				<tr><th>Player1</th><th>Player2</th><th>Bet</th><th>Status</th></tr>";
		while($data = mysqli_fetch_array($r, MYSQLI_ASSOC)){
			?>
			<tr><td><?php echo $data['player1']; ?></td><td><?php echo $data['player2']; ?></td><td><?php echo $data['bet']; ?></td><td>
				<?php 
			if($data['status'] == 'Accepted'){
				echo "Play";
			}
			else if($data['status'] == 'Pending' AND $data['player1'] == $_SESSION['username']){
				echo "<a class='btn btn-primary' href='yourgames.php?cancel=1&gameid={$data['game_id']}&pid={$data['player_id']}'>Cancel</a>";
			}
			else if($data['status'] == 'Pending' AND $data['player2'] == $_SESSION['username']){
				echo "<a class='btn btn-primary' href='yourgames.php?reject=0&gameid={$data['game_id']}&player1={$data['player1']}&player2={$data['player2']}'>Accept</a>  <a class='btn btn-primary' href='yourgames.php?reject=1&gameid={$data['game_id']}&pid={$data['player2']}'>Reject</a>";
			}
			else{
				echo "Finished";
			}
				 ?>
			</td></tr>
			<?php
		}
		echo "</table>				
				</div>";
	}
 ?>
</div>
<?php include("footer.php"); ?>